package me.wangcai.notepad.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtils {
	public static String openText(File file) {
		FileInputStream fis;
		StringBuilder sb = new StringBuilder();
		try {
			fis = new FileInputStream(file);
			byte[] bs = new byte[1024];
			int length;
			while((length = fis.read(bs)) != -1) {
				sb.append(new String(bs,0,length));
			}
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public static void saveText(File file,String str) {
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file.getName().endsWith(".txt") ? file.getPath() : file + ".txt");
			fos.write(str.getBytes());
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
